package com.cskaoyan.bean.vo.admin.auth;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/07/22:19
 * @Description:  查询用户admin信息
 */
@Component
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    MarketAdminMapper marketAdminMapper;
    @Autowired
    MarketPermissionMapper marketPermissionMapper;
    @Autowired
    MarketUserMapper userMapper;

    //先认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
        String host = token.getHost();
        String username = token.getUsername();
        //host为null代表是admin，否则为wx
        if (host == null || "".equals(host)) {
            MarketAdminExample marketAdminExample = new MarketAdminExample();
            MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
            criteria.andUsernameEqualTo(username);
            List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
            //根据username查询数据库中对应password
            if (marketAdmins != null && marketAdmins.size() == 1) {
                MarketAdmin marketAdmin = marketAdmins.get(0);
                return new SimpleAuthenticationInfo(marketAdmin, marketAdmin.getPassword(), getName());
            }
        } else {
            MarketUserExample marketUserExample = new MarketUserExample();
            marketUserExample.createCriteria().andUsernameEqualTo(username);
            List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
            //根据username查询数据库中对应password
            if (marketUsers != null && marketUsers.size() == 1) {
                MarketUser marketUser = marketUsers.get(0);
                return new SimpleAuthenticationInfo(marketUser, marketUser.getPassword(), getName());
            }
        }
        return null;
    }

    //后授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        MarketAdmin primaryPrincipal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        //获得角色id，根据角色id获取对应权限
        Integer[] roleIds = primaryPrincipal.getRoleIds();
        SimpleAuthorizationInfo simpleAuthorizationInfo;
        ArrayList<String> permissions = new ArrayList<>();
        //有多个角色时，获取权限总和
         if (roleIds.length!=1) {
             for (int i = 0; i < roleIds.length; i++) {
                 int roleId = roleIds[i];
                 MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
                 MarketPermissionExample.Criteria criteria = marketPermissionExample.createCriteria();
                 criteria.andRoleIdEqualTo(roleId);
                 List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(marketPermissionExample);
                 for (MarketPermission marketPermission : marketPermissions) {
                     String permission = marketPermission.getPermission();
                     permissions.add(permission);
                 }
             }
              simpleAuthorizationInfo = new SimpleAuthorizationInfo();
             simpleAuthorizationInfo.addStringPermissions(permissions);
         }
         //只有一个角色时
        int roleId = roleIds[0];
        MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = marketPermissionExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(marketPermissionExample);
        for (MarketPermission marketPermission : marketPermissions) {
            String permission = marketPermission.getPermission();
            permissions.add(permission);
        }
        simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }


}
